package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: ShoppingCartServiceImpl
 * Package: com.sky.service.impl
 * Description:
 *
 * @Autehor 屈子岩
 * @Create 2024/9/14 13:49
 * @Version 1.0
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private static final int NUMBER = 1;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
//        Long dishId = shoppingCartDTO.getDishId();
//        if (dishId != null){
//           ShoppingCart dish = shoppingCartMapper.getBydishIdOrsetmealId(dishId);
//           if (dish != null){
//               dish.setNumber(dish.getNumber()+1);
//               shoppingCartMapper.updateNumber(dish);
//           }else {
//               DishVO dishData = dishMapper.getById(dishId);
//               ShoppingCart shoppingCart = ShoppingCart.builder()
//                       .name(dishData.getName())
//                       .image(dishData.getImage())
//                       .amount(dishData.getPrice())
//                       .number(NUMBER)
//                       .dishFlavor(shoppingCartDTO.getDishFlavor())
//                       .dishId(dishId)
//                       .createTime(LocalDateTime.now())
//                       .userId(userId)
//                       .build();
//               shoppingCartMapper.save(shoppingCart);
//           }
//        }else {
//            ShoppingCart setmeal = shoppingCartMapper.getBydishIdOrsetmealId(shoppingCartDTO.getSetmealId());
//         if (setmeal != null){
//             setmeal.setNumber(setmeal.getNumber()+1);
//             shoppingCartMapper.updateNumber(setmeal);
//         }else {
//             SetmealVO setmealData = setmealMapper.getById(shoppingCartDTO.getSetmealId());
//             ShoppingCart shoppingCart = ShoppingCart.builder()
//                     .name(setmealData.getName())
//                     .image(setmealData.getImage())
//                     .userId(userId)
//                     .setmealId(shoppingCartDTO.getSetmealId())
//                     .number(NUMBER)
//                     .amount(setmealData.getPrice())
//                     .createTime(LocalDateTime.now())
//                     .build();
//             shoppingCartMapper.save(shoppingCart);
//         }
//        }
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartData = shoppingCartMapper.query(shoppingCart);
        if (shoppingCartData != null && shoppingCartData.size() > 0) {
            ShoppingCart cart = shoppingCartData.get(0);
            cart.setNumber(cart.getNumber() + NUMBER);
            shoppingCartMapper.update(cart);
        }
        Long dishId = shoppingCartDTO.getDishId();
        if (dishId != null) {
            DishVO dishData = dishMapper.getById(dishId);
            ShoppingCart Cart = ShoppingCart.builder()
                    .name(dishData.getName())
                    .image(dishData.getImage())
                    .amount(dishData.getPrice())
                    .number(NUMBER)
                    .dishFlavor(shoppingCartDTO.getDishFlavor())
                    .dishId(dishId)
                    .createTime(LocalDateTime.now())
                    .userId(userId)
                    .build();
            shoppingCartMapper.insert(Cart);
        } else {
            SetmealVO setmealData = setmealMapper.getById(shoppingCartDTO.getSetmealId());
            ShoppingCart Cart = ShoppingCart.builder()
                    .name(setmealData.getName())
                    .image(setmealData.getImage())
                    .userId(userId)
                    .setmealId(shoppingCartDTO.getSetmealId())
                    .number(NUMBER)
                    .amount(setmealData.getPrice())
                    .createTime(LocalDateTime.now())
                    .build();
            shoppingCartMapper.insert(Cart);
        }

    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list();
        return shoppingCarts;
    }

    /**
     * 减少购物车中的数量
     *
     * @param shoppingCartDTO
     */
    @Override
    public void subByNumber(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartData = shoppingCartMapper.query(shoppingCart);
        if (shoppingCartData != null && shoppingCartData.size() > 0) {
            ShoppingCart cart = shoppingCartData.get(0);
            cart.setNumber(cart.getNumber() - NUMBER);
            shoppingCartMapper.update(cart);
        }

    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.clean();
    }
}
